home *** CD-ROM | disk | FTP | other *** search
- Path: atglab.bls.com!Alun.Champion
- From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
- Newsgroups: comp.lang.c++
- Subject: Re: [] overload..(newbie in distress)
- Date: 23 Jan 1996 20:15:59 GMT
- Organization: Computer People Inc.
- Message-ID: <ALUN.CHAMPION.96Jan23151559@g7240065.bridge.bst.bls.com>
- References: <DLGppJ.31C@undergrad.math.uwaterloo.ca>
- <Robert.Lendvai-2101960118330001@129.170.80.94>
- <4dtuam$hf4@news.xmission.com>
- <ALUN.CHAMPION.96Jan22143422@g7240065.bridge.bst.bls.com>
- <4e12is$5no@engnews1.Eng.Sun.COM>
- NNTP-Posting-Host: bstfirewall.bst.bls.com
- In-reply-to: RAJU ALLURI's message of 22 Jan 1996 22:20:12 GMT
-
- In article <4e12is$5no@engnews1.Eng.Sun.COM> RAJU ALLURI <raju.alluri@Sun.COM> writes:
-
- : Alun.Champion@bridge.bst.bls.com (Alun Champion) wrote:
- [snip]
- :> And if it is necessary what is the argument against
- :>
- *> const T& operator [] (int index) const; (3)
- :>
- *> instead of
- :>
- *> T operator [] (int index) const; (2)
-
- [snip]
- : Then let us come to the third member function:
- : const T& operator [] (int index) const;
- : Remember, it returns a reference (even though it is a constant)
- : Most of the compilers allow you to EXPLICITLY type convert a constant
- : reference to an non-constant one, and if it is done, the constness
- : of the object is lost.
- : Hence the second construct is a better one than the third construct.
-
- [snip]
-
- : void foo( Array a, const Array ca )
- : {
- a[2] = 100; // Fine! calls first one!
- a[3] = ca[3] // Fine! calls first one on a and second one on ca!
- ca[2] = 200; // Error! return value of second [] operator is not an
- // lvalue!
- : }
-
- : Instead, if you use the third type of function for the constant objects,
- : then some wicked fellow like me can do like
- (int &)(ca[2]) = 200;
- : and make the constant object changed!
-
- This doesn't really hold much weight as some wicked fellow like me can
- circumvent the constness of the ARRAY anyway like:
-
- ((Array&)ca)[2] = 200;
-
- The third option prevents any unnecessary copying, which for large T
- or complex T with deep copy semantics can be a significant saving.
-
- Regards
-
- -A.
-
- NB [*> - means slightly changed from original post]
- --
- | A.Champion |
-